package com.huanming.dsjc.modular.university.controller;

import com.huanming.dsjc.common.constant.factory.ConstantFactory;
import com.huanming.dsjc.common.controller.BaseController;
import com.huanming.dsjc.common.exception.BizExceptionEnum;
import com.huanming.dsjc.common.exception.BussinessException;
import com.huanming.dsjc.common.persistence.dao.DeptMapper;
import com.huanming.dsjc.common.persistence.model.Dept;
import com.huanming.dsjc.core.util.ToolUtil;
import com.huanming.dsjc.modular.system.warpper.DeptWarpper;
import com.huanming.dsjc.modular.university.dao.ClassMapper;
import com.huanming.dsjc.modular.university.entity.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

/**
 * 班级管理控制器
 *
 * @author xufei
 * @Date 2018-01-24 18:07:20
 */
@Controller
@RequestMapping("/class")
public class ClassController extends BaseController {

    private String PREFIX = "/university/class/";

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 跳转到班级管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "class.html";
    }

    /**
     * 跳转到添加班级管理
     */
    @RequestMapping("/class_add")
    public String classAdd() {
        return PREFIX + "class_add.html";
    }

    /**
     * 跳转到修改班级管理
     */
    @RequestMapping("/class_update/{classId}")
    public String classUpdate(@PathVariable Integer classId, Model model) {
        Classes classes = this.classMapper.selectById(classId);
        classes.setDeptName(ConstantFactory.me().getDeptName(classes.getDeptId()));
        model.addAttribute("classes",classes);
        return PREFIX + "class_edit.html";
    }

    /**
     * 获取班级管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Classes> classesList = this.classMapper.listClasses(condition);
        for(Classes classes:classesList){
            Dept dept = deptMapper.selectById(classes.getDeptId());
            if(dept !=null ){
                classes.setDeptName(dept.getSimplename());
            }
        }
        return classesList;
    }

    /**
     * 新增班级管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Classes classes) {
        if (ToolUtil.isEmpty(classes)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        classMapper.insert(classes);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除班级管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer classId) {
        this.classMapper.deleteById(classId);
        return SUCCESS_TIP;
    }


    /**
     * 修改班级管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Classes classes) {
        if (ToolUtil.isEmpty(classes)|| classes.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        classMapper.updateById(classes);
        return super.SUCCESS_TIP;
    }

    /**
     * 班级管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
